﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace ImageManipulator
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        WriteableBitmap rawImage;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "JPG Files (*.jpg;*.jpg)|*.jpg;*.jpg | All Files (*.*)|*.*";
            ofd.FilterIndex = 1;

            if (true == ofd.ShowDialog())
            {
                System.IO.Stream stream = ofd.File.OpenRead();
                rawImage = new WriteableBitmap(0, 0);
                rawImage.SetSource(stream);
                MyImage.Source = rawImage;
                MyImage.Width = rawImage.PixelWidth;
                MyImage.Height = rawImage.PixelHeight;
                message.Text = "w:" + rawImage.PixelWidth.ToString();
                stream.Close();
                message.Text += ":h:" + rawImage.PixelHeight.ToString();
            }
        }

        public WriteableBitmap CropImage(WriteableBitmap sourceImage, RectangleGeometry cropArea)
        {
            WriteableBitmap wb = new WriteableBitmap((int)cropArea.Bounds.Width, (int)cropArea.Bounds.Height);

            TranslateTransform t = new TranslateTransform();
            t.X = cropArea.Bounds.X;
            t.Y = cropArea.Bounds.Y;

            //Draw to the Writeable Bitmap
            Image tempImage2 = new Image();
            tempImage2.Source = sourceImage;

            wb.Render(tempImage2, t);
            wb.Invalidate();

            return sourceImage;
        }

        private WriteableBitmap CropImage(WriteableBitmap sourceImage, int offsetX, int offsetY, int targetWidth, int targetHeight)
        {
            //Create a bitmap of the cropped size
            WriteableBitmap wb = new WriteableBitmap(targetWidth, targetHeight);

            //offset cop from top left of image
            TranslateTransform t = new TranslateTransform();
            t.X = offsetX;
            t.Y = offsetY;

            //Draw to the Writeable Bitmap
            Image tempImage2 = new Image();
            tempImage2.Source = sourceImage;

            wb.Render(tempImage2, t);
            wb.Invalidate();

            return wb;
        }

        private void Crop_Click(object sender, RoutedEventArgs e)
        {
            rawImage = CropImage(rawImage, -250, -250, 400, 400);

            //rawImage = CropImage(rawImage,cropArea);

            MyImage.Source = rawImage;
            MyImage.Width = 400;
            MyImage.Height = 400;

            message.Text = "x:"+rawImage.PixelHeight.ToString();
        }

        private WriteableBitmap ResizeImage(WriteableBitmap sourceImage, int targetWidth, int targetHeight)
        {
            WriteableBitmap wb = new WriteableBitmap(targetWidth, targetHeight);

            //Draw to the Writeable Bitmap
            Image tempImage2 = new Image();
            tempImage2.Width = targetWidth;
            tempImage2.Height = targetHeight;
            tempImage2.Source = sourceImage;

            wb.Render(tempImage2, null);
            wb.Invalidate();

            return wb;
        }

        private void Resize_Click(object sender, RoutedEventArgs e)
        {
            int width = 300;
            int height = 200;

            rawImage = ResizeImage(rawImage, width, height);

            //Finally set the Image back
            MyImage.Width = width;
            MyImage.Height = height;
            MyImage.Source = rawImage;
        }

        private WriteableBitmap RotateImage90Degrees(WriteableBitmap sourceImage)
        {
            //Create a bitmap of the image size
            int width = sourceImage.PixelWidth;
            int height = sourceImage.PixelHeight;
            int destWidth = width;
            int destHeight = height;
            bool landscape = true;

            if (width > height)
            {
                destHeight = width;
                destWidth = height;
            }
            else if (height > width)
            {
                landscape = false;
                destWidth = height;
                destHeight = width;
            }

            WriteableBitmap wb = new WriteableBitmap(destWidth, destHeight);

            RotateTransform rt = new RotateTransform();
            rt.Angle = 90;
            if (landscape)
                rt.CenterX = rt.CenterY = Math.Min(width / 2, height / 2);
            else
                rt.CenterX = rt.CenterY = Math.Max(height / 2, width / 2);

            //Draw to the Writeable Bitmap
            Image tempImage2 = new Image();
            tempImage2.Width = width;
            tempImage2.Height = height;
            tempImage2.Source = sourceImage;

            wb.Render(tempImage2, rt);
            wb.Invalidate();

            return wb;
        }

        private void Rotate_Click(object sender, RoutedEventArgs e)
        {
            rawImage = RotateImage90Degrees(rawImage);

            //Finally set the Image back
            MyImage.Width = rawImage.PixelWidth;
            MyImage.Height = rawImage.PixelHeight;
            MyImage.Source = rawImage;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {

        }

        private WriteableBitmap MatrixTransform(WriteableBitmap sourceImage, Matrix matrix)
        {
            //Create a bitmap of the cropped size
            WriteableBitmap wb = new WriteableBitmap(sourceImage.PixelWidth, sourceImage.PixelHeight);

            //offset cop from top left of image
            MatrixTransform mt = new MatrixTransform();
            mt.Matrix = matrix;

            //Draw to the Writeable Bitmap
            Image tempImage2 = new Image();
            tempImage2.Source = sourceImage;

            wb.Render(tempImage2, mt);
            wb.Invalidate();

            return wb;
        }

        private WriteableBitmap FlipHorizontal(WriteableBitmap sourceImage)
        {
            Matrix matrix = new Matrix(-1, 0, 0, 1, sourceImage.PixelWidth, 0);
            return MatrixTransform(sourceImage, matrix);
        }

        private WriteableBitmap FlipVertical(WriteableBitmap sourceImage)
        {
            Matrix matrix = new Matrix(1, 0, 0, -1, 0, sourceImage.PixelHeight);
            return MatrixTransform(sourceImage, matrix);
        }

        private void Flip_Horizontal_Click(object sender, RoutedEventArgs e)
        {
            rawImage = FlipHorizontal(rawImage);

            //Finally set the Image back
            MyImage.Width = rawImage.PixelWidth;
            MyImage.Height = rawImage.PixelHeight;
            MyImage.Source = rawImage;
        }

        private void Flip_Vertical_Click(object sender, RoutedEventArgs e)
        {
            rawImage = FlipVertical(rawImage);

            //Finally set the Image back
            MyImage.Width = rawImage.PixelWidth;
            MyImage.Height = rawImage.PixelHeight;
            MyImage.Source = rawImage;
        }
    }
}
